<?php
class SearchIndexesController extends AppController {

    public $name = 'SearchIndexes';
    public $paginate = array('SearchIndex' => array('limit' => 10));
    public $helpers = array('Searchable');

    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('index');
    }

    public function index(){
        // Redirect with search data in the URL in pretty format
        $redirect = array();
        if(!empty($this->data)){
            if(isset($this->data['SearchIndex']['term']) && !empty($this->data['SearchIndex']['term'])){
                $redirect['term'] = urlencode(urlencode($this->data['SearchIndex']['term']));
            }else{
                $redirect['term'] = 'null';
            }
            if(isset($this->data['SearchIndex']['type']) && !empty($this->data['SearchIndex']['type'])){
                $redirect['type'] = $this->data['SearchIndex']['type'];
            }else{
                $redirect['type'] = 'All';
            }
            $this->redirect($redirect);

        }

        /* Reset search conditions, we use AND and OR */
        $this->paginate['SearchIndex']['conditions']['AND'] = array();
        $this->paginate['SearchIndex']['conditions']['OR'] = array();

        // Add default scope condition
        $this->paginate['SearchIndex']['conditions'] = array('SearchIndex.active' => 1);

        // Add published condition NULL or < NOW()
        $this->paginate['SearchIndex']['conditions']['OR'] = array(
            array('SearchIndex.published' => null),
            array('SearchIndex.published <= ' => date('Y-m-d H:i:s'))
        );

        // Add type condition if not All
        if(isset($this->params['type']) && $this->params['type'] != 'All'){
            $this->data['SearchIndex']['type'] = $this->params['type'];
            $this->paginate['SearchIndex']['conditions']['model'] = $this->params['type'];
        }

        /* Add term condition, and sorting */
        if(isset($this->params['term']) && !empty($this->params['term'])){
            $term = $this->params['term'] = trim($this->params['term']);
            App::import('Core', 'Sanitize');
            /* Handle multiple keywords */
            if(preg_match('/[\s,]+/', $term)){
                $this->log('Search with multiple keywords:' . $term);
                $term = preg_split('/[\s,]+/', $term);
                $term = array_map('trim', $term);
                foreach($term as $tm){
                    $tm = Sanitize::escape($tm);
                    $this->paginate['SearchIndex']['conditions']['AND'][] = "data LIKE '%$tm%'";
                }
            }else{
                $term = Sanitize::escape($term);
                $this->paginate['SearchIndex']['conditions'][] = "data LIKE '%$term%'";
            }
            $this->data['SearchIndex']['term'] = (array)$term;
            $this->paginate['SearchIndex']['order'] = "modified DESC";
        }

        if(!empty($term)){
            $results = $this->paginate();
        }

        // Get types for select drop down
        $types = $this->SearchIndex->getTypes();
        if(in_array($this->user['group'], array(null, 'student')) && isset($types['Student'])){
            unset($types['Student']);
        }
        $term = empty($term) ? '' : implode(',', (array) $term);
        $this->set(compact('results', 'types', 'term'));

    }

}
?>